﻿namespace Jhong.FunMVCBlog.Admin.Controllers
{
    using Jhong.FunMVC;
    using Jhong.FunMVCBlog.Admin.Model;
    using Jhong.FunMVCBlog.Core.Data;
    using Jhong.FunMVCBlog.Core.Infrastructure;
    using System;
    using System.IO;

    public class SettingController : SecurityController
    {
        public ActionResult Index()
        {
            /***********************/
            var bloginfo = new BlogInfoViewModel();
            bloginfo.NickName = this._sysData.GetNickName();
            bloginfo.BlogName = this._sysData.GetBlogName();
            bloginfo.About = this._sysData.GetAbout();
            bloginfo.HomePage = this._sysData.GetHomePage();
            ViewData["bloginfo"] = bloginfo;
            /***********************/

            return View();
        }

        private BlogSysData _sysData = new BlogSysData();

        [HttpPost]
        public ActionResult ReSetPwd(string spwd, string account, string npwd, string rpwd)
        {
            if (npwd != rpwd) return AjaxResult(false, "两次密码不同，修改失败");
            if (false == this._sysData.GetPassword().Equals(spwd.GetMD5())) return AjaxResult(false, "原密码错误，修改失败");
            this._sysData.SetPassword(npwd.GetMD5());
            this._sysData.SetAccount(account);
            return AjaxResult(true, "更新成功");
        }

        public ActionResult ChangePersonThumbnail()
        {
            var file = Request.Forms.Files[0];
            var filePath = Path.Combine("Static", "PersonThumbnail", Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName)).Replace("\\", "/");
            file.SaveAs(Path.Combine(WebSiteRootPath, filePath));
            this._sysData.SetPersonThumbnail(filePath.StartsWith("/") ? filePath : "/" + filePath);
            return Content("<script>alert('修改成功');window.location.href='/AHome/Index';</script>");
        }

        public ActionResult ChangeBlogInfo(BlogInfoViewModel model)
        {
            this._sysData.SetBlogName(model.BlogName);
            this._sysData.SetNickName(model.NickName);
            this._sysData.SetAbout(model.About);
            this._sysData.SetHomePage(model.HomePage);
            return AjaxResult(true, "修改成功");
        }
    }
}